
function getReverse(str) {
    let len = str.length;
    if (len === 0) return '';
    let arr = str.split('');
    let count = 0;
    let map = {};
    let maxCount = 0;
    for (let i = 0; i < len; i++) { //建立每一个count对应的字母下标索引 { '1': [ 1, 2, 13, 14 ], '2': [ 4, 5, 10, 11 ], '3': [ 7, 8 ] }
        if (arr[i] === '(') {
            count++;
            maxCount = Math.max(maxCount, count);
        } else if (arr[i] === ')') {
            count--;
            // map.set(i,-2)
        } else {
            if (!map[count]) map[count] = [];
            map[count].push(i);

        }
    }
    console.log(map);
    let ans = [];
    let mid = 0;
    for (let i = maxCount; i >= 0; i--) { //对于每一个count，判断是偶数还是奇数
        if (!map[i]) continue;
        let temp = map[i];
        let lent = temp.length;
        if (i % 2 === 0) {
            if (ans.length === 0) {
                for (let i = 0; i < lent; i++) {
                    ans.push(arr[temp[i]]);
                }
                mid = temp[0];
            } else {
                let temparr = [];
                for (let i = 0; i < lent; i++) {
                    if (temp[i] < mid) {
                        temparr.push(arr[temp[i]]);
                    } else {
                        ans.push(arr[temp[i]])
                    }
                }
                ans = temparr.concat(ans);
            }

        } else {
            if (ans.length === 0) {
                for (let i = lent; i >= 0; i--) {
                    ans.push(arr[temp[i]]);
                }
                mid = temp[0];
            } else {
                let tempc = [];
                for (let i = 0; i < lent; i++) {
                    if (temp[i] < mid) {
                        tempc.unshift(arr[temp[i]]);
                        // ans.push(arr[temp[i]]);
                    } else {
                        ans.unshift(arr[temp[i]])
                    }
                }
                ans = ans.concat(tempc);
            }

        }
    }

    console.log(ans.join(''));

}
console.log(getReverse('(12(34(5678()())9x)'))
console.log(getReverse('(abs())'))
console.log(getReverse('abd'))
// x934657821

